// var sub;//top.sys.userName(1);
var base;
$(function () {
    // sub = subInfo();
    base = init();

    function init() {
        return {
            getQueryString: function (field, url) {
                var href = url ? url : window.location.href;
                var reg = new RegExp('[?&]' + field + '=([^&]*)', 'i');
                var string = reg.exec(href);
                return string ? string[1] : null;
            },
            dict: {
                resetDict: function (id, targetId) {//目标option value  包含源 value
                    var val = $('#' + id).val();
                    var $select = $('#' + targetId);
                    checkSelect(targetId);
                    $('#' + targetId).empty();
                    $($('#copy_' + targetId + ' option')).each(function (index, element) {
                        var elevar = $(this).val();
                        if (elevar.indexOf(val) > -1 || val == '' || index == 0) {
                            $select.append($(this).prop("outerHTML"));
                        }
                    });
                },
                resetDict0: function (id, targetId) {//目标option value 等于 源 value
                    var val = $('#' + id).val();
                    var $select = $('#' + targetId);
                    checkSelect(targetId);
                    $('#' + targetId).empty();
                    $($('#copy_' + targetId + ' option')).each(function (index, element) {
                        var supervalue = $(this).attr('supervalue');
                        if (val == supervalue || val == '' || index == 0) {
                            $select.append($(this).prop("outerHTML"));
                        }
                    });
                },
                resetDictByDept: function (id, targetId) {//源 value (deptId) 是目标option value 的上级
                    var val = $('#' + id).val();
                    val = ',' + top.sys.deptAttr(val, 'childrens') + ',';
                    var $select = $('#' + targetId);
                    checkSelect(targetId);
                    $('#' + targetId).empty();
                    $($('#copy_' + targetId + ' option')).each(function (index, element) {
                        var supervalue = $(this).attr('supervalue');
                        supervalue = ',' + supervalue + ','
                        if (val.indexOf(supervalue) > -1 || val == '' || index == 0) {
                            $select.append($(this).prop("outerHTML"));
                        }
                    });
                }
            },
            date: {
                getDate: function (day) {//yyyy-MM-dd hh:mm:ss 获取day天后的时间
                    var dd = new Date();
                    dd.setDate(dd.getDate() + day);
                    var y = dd.getFullYear();
                    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
                    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
                    return y + "-" + m + "-" + d;
                },
                getDateTime: function (day) {//yyyy-MM-dd hh:mm:ss 获取day天后的时间
                    var dd = new Date();
                    dd.setDate(dd.getDate() + day);
                    var y = dd.getFullYear();
                    var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
                    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
                    var h = dd.getHours() < 10 ? "0" + dd.getHours() : dd.getHours(); //获取当前小时数(0-23)
                    var mm = dd.getMinutes() < 10 ? "0" + dd.getMinutes() : dd.getMinutes(); //获取当前分钟数(0-59)
                    var s = dd.getSeconds() < 10 ? "0" + dd.getSeconds() : dd.getSeconds(); //获取当前秒数(0-59)
                    return y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
                }
            }
        };
    }

    function checkSelect(targetId) {
        if ($("#copy_" + targetId).length == 0) {
            var optionHtml = $("#" + targetId).html();
            var copyHtml = '<select id="copy_' + targetId + '" style="display:none" >' + optionHtml + '</select>';
            $("#" + targetId).after(copyHtml);
        }
    }
});
